/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;
import negocio.Proveedor;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author JAVA
 */
public class PersistenciaProveedor {
    
    
    
    public static int crearProveedor(Proveedor p){
        int i=0;
        Connection con=null;
        Statement st=null;
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con=DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub","cibernos","1234");
                st=con.createStatement();
                i=st.executeUpdate("insert into Proveedor (id,nombre) VALUES ('0','"+p.getNombre()+"')");
            } catch (SQLException ex) {
                Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally{
                st.close();
                con.close();
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return i;
        }
    }
    
    public static int eliminarProveedor(Proveedor p){
        int i=0;
        Connection con=null;
        Statement st=null;
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con=DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub","cibernos","1234");
                st=con.createStatement();
                i=st.executeUpdate("update Proveedor set baja='1' where Proveedor.id='"+p.getId()+"';");
            } catch (SQLException ex) {
                Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally{
                st.close();
                con.close();
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{          
            return i;
        }
    }
    
    public static int modificarProveedor(Proveedor p){
        int n=0;
        try {
            
            Connection con=null;
            Statement st=null;
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub","cibernos","1234");
            st=con.createStatement();
            n=st.executeUpdate("update Proveedor set nombre='"+p.getNombre()+"' where Proveedor.id='"+p.getId()+"'");           
            st.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return n;
        }
    }
    
    public static List<Proveedor> devolverProveedores(){
        List prov=new ArrayList<Proveedor>();
        Connection con=null;
        Statement st=null;
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con=DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub","cibernos","1234");
                st=con.createStatement();
                ResultSet r=st.executeQuery("select id,nombre from Proveedor where proveedor.baja='0'");
                while(r.next()){
                    Proveedor p=new Proveedor(r.getInt("id"),r.getString("nombre"));
                    prov.add(p);
                }
            } catch (SQLException ex) {
                Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally{
                st.close();
                con.close();
                
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return prov;
        }
    }
    
    public static Proveedor obtenerProveedor(int codigo){
        Proveedor p=null;
        Connection con=null;
        Statement st=null;
        try {   
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub","cibernos","1234");
            st=con.createStatement();
            ResultSet r=st.executeQuery("Select id,nombre from proveedor where proveedor.id='"+codigo+"' and proveedor.baja='0'");
            while(r.next()){
                p=new Proveedor(r.getInt("id"),r.getString("nombre"));
            }
            st.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return p;
        }
        
        
    }
}
